package com.zlx.sharelive.activity;

import android.graphics.Typeface;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_fg.BaseFg;
import com.zlx.sharelive.R;
import com.zlx.sharelive.fragment.LikeGraphicFg;
import com.zlx.sharelive.fragment.LikeArticleFg;
import com.zlx.sharelive.fragment.LikeFabulousShortVideoFg;
import com.zlx.sharelive.fragment.LikeLongVideoFg;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 点赞
 */
public class LikeAc extends BaseAc {

    @BindView(R.id.tv_long_video)
    TextView tv_long_video;
    @BindView(R.id.tv_short_video)
    TextView tv_short_video;
    @BindView(R.id.tv_article)
    TextView tv_article;
    @BindView(R.id.tv_graphic)
    TextView tv_graphic;
    @BindView(R.id.iv_long_video)
    ImageView iv_long_video;
    @BindView(R.id.iv_short_video)
    ImageView iv_short_video;
    @BindView(R.id.iv_article)
    ImageView iv_article;
    @BindView(R.id.iv_graphic)
    ImageView iv_graphic;

    private List<TextView> mTextViewList = new ArrayList<>();
    private List<ImageView> mImageViewList = new ArrayList<>();
    private List<BaseFg> mFragment = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.ac_like;
    }

    @Override
    public void initViews() {
        super.initViews();

        mTextViewList.clear();
        mTextViewList.add(tv_long_video);
        mTextViewList.add(tv_short_video);
        mTextViewList.add(tv_article);
        mTextViewList.add(tv_graphic);
        mImageViewList.clear();
        mImageViewList.add(iv_long_video);
        mImageViewList.add(iv_short_video);
        mImageViewList.add(iv_article);
        mImageViewList.add(iv_graphic);
        initFragment();
    }

    private void initFragment() {
        mFragment.clear();
        mFragment.add(new LikeLongVideoFg());
        mFragment.add(new LikeFabulousShortVideoFg());
        mFragment.add(new LikeArticleFg());
        mFragment.add(new LikeGraphicFg());
        commitAllowingStateLoss(0);
    }

    private void commitAllowingStateLoss(int position) {
        hideAllFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(position + "");
        if (currentFragment != null) {
            transaction.show(currentFragment);
        } else {
            currentFragment = mFragment.get(position);
            transaction.add(R.id.frameLayout, currentFragment, position + "");
        }
        transaction.commitAllowingStateLoss();
    }

    /**
     * 隐藏所有Fragment
     */
    private void hideAllFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        for (int i = 0; i < mFragment.size(); i++) {
            Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(i + "");
            if (currentFragment != null) {
                transaction.hide(currentFragment);
            }
        }
        transaction.commitAllowingStateLoss();
    }

    @OnClick({R.id.iv_back, R.id.ll_long_video, R.id.ll_short_video, R.id.ll_article, R.id.ll_graphic})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_long_video:
                commitAllowingStateLoss(0);
                setTextViewColor(0);
                break;
            case R.id.ll_short_video:
                commitAllowingStateLoss(1);
                setTextViewColor(1);
                break;
            case R.id.ll_article:
                commitAllowingStateLoss(2);
                setTextViewColor(2);
                break;
            case R.id.ll_graphic:
                commitAllowingStateLoss(3);
                setTextViewColor(3);
                break;

        }
    }

    public void setTextViewColor(int position) {
        for (int i = 0; i < mTextViewList.size(); i++) {
            if (position == i) {
                mTextViewList.get(position).setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                mImageViewList.get(position).setVisibility(View.VISIBLE);
            } else {
                mTextViewList.get(i).setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                mImageViewList.get(i).setVisibility(View.INVISIBLE);
            }
        }
    }
}
